﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GameControls;
using Microsoft.Xna.Framework.Graphics;
using ThouShaltNotBrick.Logic.Helpers;

namespace ThouShaltNotBrick.Logic.GameStates
{
    internal sealed class PlayLossGameState : PlayGameStateBase
    {
        #region Constants
        #endregion

        #region Properties
        private Label mLivesLabel;
        private Button mContinueButton;
        #endregion

        #region Events
        #endregion

        #region Constructors
        #endregion

        #region Overriden Methods
        protected override void InitializeImpl()
        {
            base.InitializeImpl();

            mIsMouseVisible = true;

            InitializeControls();
        }

        protected override void DestroyImpl()
        {
            DestroyControls();

            base.DestroyImpl();
        }

        protected override void StartImpl(GameStateChangeData pChangeData)
        {
            SetPlayData(pChangeData);

            if (mPlayData.Lives <= 0)
            {
                GameStateChangeData gscd = new GameStateChangeData(mPlayData);
                GameStateManager.ChangeState(GameStateType.Loss, gscd);
            }

            mLivesLabel.Text = "Remaining lives: " + mPlayData.Lives;
        }

        protected override void StopImpl()
        {
        }

        protected override void ResumeImpl(GameStateChangeData pChangeData)
        {
            SetPlayData(pChangeData);
        }

        protected override void PauseImpl()
        {
        }

        protected override void HandleInputImpl(GameTime pGameTime)
        {
        }

        protected override void UpdateImpl(GameTime pGameTime)
        {
        }

        protected override void DrawImpl(GameTime pGameTime)
        {
            mLevelDrawer.Draw(pGameTime, mPlayData);
        }
        #endregion

        #region Methods
        private void InitializeControls()
        {
            DestroyControls();

            int screenWidth = mGameManager.GetClientWidth();
            int screenHeight = mGameManager.GetClientHeight();

            Rectangle shadeImageRect = new Rectangle(0, 0, screenWidth, screenHeight);
            Image shadeImage = new Image(shadeImageRect, mGraphicsResources.ShadeTexture);
            mControls.Add(shadeImage);

            SpriteFont lostALifeFont = mFontResources.Font20;
            int lostALifeLabelHeight = (int)lostALifeFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle lostALifeLabelRect = new Rectangle(0, screenHeight / 2 - lostALifeLabelHeight, screenWidth, lostALifeLabelHeight);
            Label lostALifeLabel = new Label(lostALifeLabelRect);
            lostALifeLabel.SetText("You have lost a life!", lostALifeFont, Color.Red);
            lostALifeLabel.HorizontalAlignment = HorizontalAlignment.Center;
            lostALifeLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(lostALifeLabel);

            SpriteFont livesFont = mFontResources.Font20;
            int livesLabelHeight = (int)livesFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle livesLabelRect = new Rectangle(0, screenHeight / 2, screenWidth, livesLabelHeight);
            mLivesLabel = new Label(livesLabelRect);
            mLivesLabel.SetText(string.Empty, livesFont, Color.Red);
            mLivesLabel.HorizontalAlignment = HorizontalAlignment.Center;
            mLivesLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(mLivesLabel);

            SpriteFont continueButtonFont = mFontResources.Font20;
            int continueButtonWidth = 200;
            int continueButtonHeight = (int)continueButtonFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle continueButtonRect = new Rectangle(screenWidth / 2 - continueButtonWidth / 2, livesLabelRect.Bottom, continueButtonWidth, continueButtonHeight);
            mContinueButton = new Button(continueButtonRect, mGraphicsResources.WhiteTexture);
            mContinueButton.Label.SetText("Continue", continueButtonFont, Color.Red);
            mContinueButton.Pressed += ContinueButtonPressedHandler;
            mControls.Add(mContinueButton);
        }

        private void DestroyControls()
        {
            if (mContinueButton != null)
            {
                mContinueButton.Pressed -= ContinueButtonPressedHandler;
            }

            mControls.Clear();
        }
        #endregion

        #region Event Handlers
        private void ContinueButtonPressedHandler(object sender, EventArgs e)
        {
            mPlayData.CurrentLevel.ResetPadAndBall();
            ChangeToAnotherPlayState(GameStateType.PlayNormal);
        }
        #endregion

        #region Inner Classes
        #endregion
    }
}